package com.spice.service.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.spice.common.model.BaseEntity;
import com.spice.service.entity.pojo.PackMaintain;
import com.spice.service.entity.pojo.ShapeDesc;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;

import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExcelUtil {

    public static void write(String filePath, Class<?> cls, List<?> list) {
        String fileName = new File(filePath).getAbsolutePath();
        EasyExcel.write(fileName, cls)
                .sheet("sheetName")
                .doWrite(list);
    }


    public static List<BaseEntity> read(String filePath, Class<?> cls) {
        List<BaseEntity> list = new ArrayList<>();
        EasyExcel.read(new File(filePath).getAbsolutePath(),
                cls,
                new AnalysisEventListener<BaseEntity>() {
                    //每读取一行就调用该方法
                    @Override
                    public void invoke(BaseEntity data, AnalysisContext context) {
                        System.out.println(data);
                        list.add(data);
                    }

                    // 全部读取完成就调用该方法
                    @Override
                    public void doAfterAllAnalysed(AnalysisContext context) {
                        log.info("读取Excel完成,行数：" + list.size());
                    }
                }).sheet().doRead();
        return list;
    }
}
